package com.aids_malaria.util;

import com.aids_malaria.db.DBManager;
import com.aids_malaria.http.response.LoginUserAuth;

import java.util.List;

/**
 *      * insert : 1
 *      * update : 1
 *      * inq : 1
 *      * delete : 1
 *      * upload : 0
 *      * download : 0
 *
 *      eg: boolean haveInsertAuthByCode = AuthUtil.isHaveInsertAuthByCode(Constant.SETTING_USER_MANAGER);
 */
public class AuthUtil {


    /**
     * 是否有指定插入权限
     * @param authCode
     * @return
     */
    public static boolean isHaveInsertAuthByCode(String authCode){
        List<LoginUserAuth> loginUserAuthList = DBManager.get().loginUserAuthDao().findByAuthCode(authCode);
        if(loginUserAuthList.size() > 0){
            return loginUserAuthList.get(0).getInsert() == 1;
        }else{
            //没有数据则无权限
            return false;
        }
    }

    /**
     * 是否有指定更新权限
     * @param authCode
     * @return
     */
    public static boolean isHaveUpdateAuthByCode(String authCode){
        List<LoginUserAuth> loginUserAuthList = DBManager.get().loginUserAuthDao().findByAuthCode(authCode);
        if(loginUserAuthList.size() > 0){
            return loginUserAuthList.get(0).getUpdate() == 1;
        }else{
            //没有数据则无权限
            return false;
        }
    }

    /**
     * 是否有指定查询权限
     * @param authCode
     * @return
     */
    public static boolean isHaveQueryAuthByCode(String authCode){
        List<LoginUserAuth> loginUserAuthList = DBManager.get().loginUserAuthDao().findByAuthCode(authCode);
        if(loginUserAuthList.size() > 0){
            return loginUserAuthList.get(0).getInq() == 1;
        }else{
            //没有数据则无权限
            return false;
        }
    }

    /**
     * 是否有指定删除权限
     * @param authCode
     * @return
     */
    public static boolean isHaveDeleteAuthByCode(String authCode){
        List<LoginUserAuth> loginUserAuthList = DBManager.get().loginUserAuthDao().findByAuthCode(authCode);
        if(loginUserAuthList.size() > 0){
            return loginUserAuthList.get(0).getDelete() == 1;
        }else{
            //没有数据则无权限
            return false;
        }
    }

    /**
     * 是否有指定上传权限
     * @param authCode
     * @return
     */
    public static boolean isHaveUploadAuthByCode(String authCode){
        List<LoginUserAuth> loginUserAuthList = DBManager.get().loginUserAuthDao().findByAuthCode(authCode);
        if(loginUserAuthList.size() > 0){
            return loginUserAuthList.get(0).getUpload() == 1;
        }else{
            //没有数据则无权限
            return false;
        }
    }

    /**
     * 是否有指定下载权限
     * @param authCode
     * @return
     */
    public static boolean isHaveDownloadAuthByCode(String authCode){
        List<LoginUserAuth> loginUserAuthList = DBManager.get().loginUserAuthDao().findByAuthCode(authCode);
        if(loginUserAuthList.size() > 0){
            return loginUserAuthList.get(0).getDownload() == 1;
        }else{
            //没有数据则无权限
            return false;
        }
    }

    public static boolean isHaveAuthByCode(String authCode){
        List<LoginUserAuth> loginUserAuthList = DBManager.get().loginUserAuthDao().findByAuthCode(authCode);
        if(loginUserAuthList.size() > 0){
            LoginUserAuth loginUserAuth = loginUserAuthList.get(0);
            //全部禁止则视为无权限
            if(loginUserAuth.getInsert() == 0 && loginUserAuth.getDelete() == 0 &&
                    loginUserAuth.getInq() == 0 && loginUserAuth.getUpdate() == 0 &&
                    loginUserAuth.getDownload() == 0 && loginUserAuth.getUpdate() == 0){
                return false;
            }else{
                return true;
            }
        }else{
            //没有数据则无权限
            return false;
        }
    }

}
